<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Cart as ModelCart;
use app\common\model\forest\Member;
use app\common\model\Goods;
use think\cache\driver\Redis;

/**
 * 购物车接口
 */
class Cart extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];


    /**
     * 添加/编辑购物车
     *
     */
    public function addCart()
    {
        $param = $this->request->post();
        $param['uid'] = $this->auth->id;
        try {
            $res = ModelCart::addCart($param);
        } catch (\Exception $e) {
            $this->error('失败:' . $e->getMessage());
        }
        $this->success('成功', $res);
    }
    /**
     * 删除购物车
     *
     */
    public function delCart()
    {
        $post = $this->request->post();
        extract($post);
        if ((!isset($cart_ids) && empty($cart_ids))) {
            $this->error('参数错误');
        }
        $res = ModelCart::delCart($this->auth->id, $cart_ids);
        if ($res)
            $this->success('删除成功');
        $this->error('删除失败');
    }
    /**
     * 查看购物车
     *
     */
    public function cartList()
    {
        $uid = $this->auth->id;
        $data = ModelCart::list($uid);
        $cloumn = array_column($data, 'num');
        $this->success('获取成功', ['data' => $data, 'count' => array_sum($cloumn)]);
    }
}
